using System;
using System.Text;
using System.Globalization;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;
using System.Configuration;
using Microsoft.SharePoint.Utilities;
using System.Reflection;
using System.Collections;
using System.Diagnostics;

namespace CKS.FormsBasedAuthentication
{   
    /// <summary>
    /// This class is a feature activation event handler for the Self Management feature. It (de)activates/(un)installs all the sub features of related to FBA Self Management
    /// </summary>
    class SelfManagementFeatureReceiver : SPFeatureReceiver
    {
        private Guid _solutionID = new Guid("EC69235D-7FA1-4382-B7F8-0E7029DAEDA9");
        private Dictionary<String, Guid> _features = new Dictionary<String, Guid>();
        
        public SelfManagementFeatureReceiver()
        {   
            _features.Add("LoginWebPart", new Guid("311B4577-4677-459d-9B10-8989DA574017")); // login webpart
            _features.Add("MembershipReviewList", new Guid("69CE2076-9A2F-4c71-AEDF-F4252C01DE4E")); // membership list
            _features.Add("MembershipRequestWebPart", new Guid("68080064-E494-4807-A6A7-4A92DEDD7CE5")); // membership request webpart
            _features.Add("PasswordRecoveryWebPart", new Guid("39855F49-B8E5-492a-AFE8-FE93BF139F57")); // password recovery
            _features.Add("ChangePasswordWebPart", new Guid("8AF74562-1972-4373-A427-EA9F700088EC")); // change password
            _features.Add("ChangePasswordPage", new Guid("414C8D88-C11B-43b0-A809-1D5118C790EA")); // change password page
            _features.Add("FBASaveAction", new Guid("01AA8D8B-000A-4c35-8F4F-5D1280377650")); // save action
        }

        public override void FeatureActivated(SPFeatureReceiverProperties properties) 
        {
            // get site reference
            SPSite site = properties.Feature.Parent as SPSite;

            // activate sub features            
            bool activate = true;            
            Utils.ToggleFeatureActivation(site, _features, activate);
            
            // update layouts bread crumb
            this.UpdateLayoutSiteMap(site);

            site.Dispose();
        }
        

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties) 
        {
            // get site reference
            SPSite site = properties.Feature.Parent as SPSite;

            // deactivate sub features
            bool activate = false;
            Utils.ToggleFeatureActivation(site, _features, activate);
            
            site.Dispose();      
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            // install sub features
            bool install = true;
            Utils.ToggleFeatureInstallation(_features, _solutionID, install);
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            // uninstall sub features
            bool install = false;
            Utils.ToggleFeatureInstallation(_features, _solutionID, install);
        }

        private void UpdateLayoutSiteMap(SPSite site)
        {
            // update layouts site map
            try
            {
                UpdateLayoutsSitemap uls = new UpdateLayoutsSitemap(site.WebApplication);
                uls.AddSitemap("layouts.sitemap.FBASelfManagement.xml");
                uls.SubmitJob();
            }
            catch (Exception ex)
            {
                Utils.LogError(ex);
            }
        }       
    }
}
